﻿using Infrastructure;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OpenAuth.App.Report
{
    public class InspectViolationReport : BaseApp<InspectPlan>
    {
        private IAuth _auth;
        public InspectViolationReport(IUnitWork unitWork, IRepository<InspectPlan> repository, RevelanceManagerApp app, IAuth auth) : base(unitWork, repository)
        {
            _auth = auth;
        }

        public List<InspectViolationReportResp> LoadData(InspectViolationReportReq req)
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null)
            {
                throw new CommonException("登录已过期", Define.INVALID_TOKEN);
            }

            req.startTime = req.startTime?.ToString("yyyy-MM-dd 00:00:00").ToDateTime() ??
                            DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd 00:00:00").ToDateTime();
            req.endTime = req.endTime?.AddDays(1).ToString("yyyy-MM-dd 00:00:00").ToDateTime() ??
                          DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00").ToDateTime();
            req.endTime = req.endTime?.AddMilliseconds(1);
            List<string> orgIdList = new List<string>();
            if (!string.IsNullOrWhiteSpace(req.orgId))
            {
                var org = loginContext.Orgs.SingleOrDefault(u => u.Id == req.orgId);
                foreach (Org item in loginContext.Orgs)
                {
                    if (item.CascadeId.Contains(org.CascadeId))
                    {
                        orgIdList.Add(item.Id);
                    }
                }
            }
            else
            {
                orgIdList = loginContext.Orgs.Select(u => u.Id).ToList();
            }

            var plans = UnitWork.Find<InspectPlan>(u => orgIdList.Contains(u.OrgId)).ToList();
            var violations = UnitWork.Find<InspectViolation>(u => u.CreateTime >= req.startTime && u.CreateTime < req.endTime).ToList();
            List<InspectViolationReportResp> result = new List<InspectViolationReportResp>();
            foreach (var plan in plans)
            {
                var notInspecting = violations.Count(u => u.InspectPlanId == plan.Id && u.Category == 0 && u.IsViolation);
                var timeOut = violations.Count(u => u.InspectPlanId == plan.Id && u.Category == 1 && u.IsViolation);
                var missArea = violations.Count(u => u.InspectPlanId == plan.Id && u.Category == 2 && u.IsViolation);
                var resp = new InspectViolationReportResp
                {
                    PlanId = plan.Id,
                    PlanName = plan.Name,
                    NotInspecting = notInspecting,
                    TimeOut = timeOut,
                    MissArea = missArea
                };
                result.Add(resp);
            }
            return result;
        }


    }
}
